# -*- coding: utf-8 -*-

################################################################################
## Form generated from reading UI file '选项卡的动态添加和删除.ui'
##
## Created by: Qt User Interface Compiler version 5.15.2
##
## WARNING! All changes made in this file will be lost when recompiling UI file!
################################################################################

from PySide2.QtCore import *
from PySide2.QtGui import *
from PySide2.QtWidgets import *
import sys
from PySide2 import QtWidgets, QtGui, QtCore



class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        if not MainWindow.objectName():
            MainWindow.setObjectName(u"MainWindow")
        MainWindow.resize(800, 600)
        self.centralwidget = QWidget(MainWindow)
        self.centralwidget.setObjectName(u"centralwidget")
        self.tabWidget = QTabWidget(self.centralwidget)
        self.tabWidget.setObjectName(u"tabWidget")
        self.tabWidget.setGeometry(QRect(180, 80, 421, 281))
        self.tab = QWidget()
        self.tab.setObjectName(u"tab")
        self.tabWidget.addTab(self.tab, "")
        self.tab_2 = QWidget()
        self.tab_2.setObjectName(u"tab_2")
        self.tabWidget.addTab(self.tab_2, "")
        self.pushButton = QPushButton(self.centralwidget)
        self.pushButton.setObjectName(u"pushButton")
        self.pushButton.setGeometry(QRect(250, 420, 93, 28))
        self.pushButton_2 = QPushButton(self.centralwidget)
        self.pushButton_2.setObjectName(u"pushButton_2")
        self.pushButton_2.setGeometry(QRect(470, 420, 93, 28))

        # 分别为”添加“”删除“以及选项卡的currentChanged信号绑定自定义的槽函数
        self.pushButton.clicked.connect(self.addtab)
        self.pushButton_2.clicked.connect(self.dettab)
        self.tabWidget.currentChanged.connect(self.gettab)

        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QMenuBar(MainWindow)
        self.menubar.setObjectName(u"menubar")
        self.menubar.setGeometry(QRect(0, 0, 800, 26))
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QStatusBar(MainWindow)
        self.statusbar.setObjectName(u"statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)

        QMetaObject.connectSlotsByName(MainWindow)
    # setupUi

    def retranslateUi(self, MainWindow):
        MainWindow.setWindowTitle(QCoreApplication.translate("MainWindow", u"\u9009\u9879\u5361\u7684\u52a8\u6001\u6dfb\u52a0\u548c\u5220\u9664", None))
        self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab), QCoreApplication.translate("MainWindow", u"Tab 1", None))
        self.tabWidget.setTabText(self.tabWidget.indexOf(self.tab_2), QCoreApplication.translate("MainWindow", u"Tab 2", None))
        self.pushButton.setText(QCoreApplication.translate("MainWindow", u"\u6dfb\u52a0", None))
        self.pushButton_2.setText(QCoreApplication.translate("MainWindow", u"\u5220\u9664", None))
    # retranslateUi
    # 新增选项卡
    def addtab(self):
        self.atab = QtWidgets.QWidget()       # 创建选项卡对象
        name = "Tab "+str(self.tabWidget.count()+1)
        self.atab.setObjectName(name)
        self.tabWidget.addTab(self.atab, name)   # 将选项卡对象和对应选项卡的名字 添加到 列表控件当中

    # 删除选项卡
    def dettab(self):
        self.tabWidget.removeTab(self.tabWidget.currentIndex())

    # 获取选中的选项卡及索引
    def gettab(self, currentIndex):
        from PySide2.QtWidgets import QMessageBox
        QMessageBox.information(MainWindow, "提示", "您选择了"+self.tabWidget.tabText(currentIndex)+"选项卡，索引为：" +
                                str(self.tabWidget.currentIndex()), QMessageBox.Ok)


if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())


